package com.kgc.sbt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.sbt.api.CouponApi;
import com.kgc.sbt.api.CouponLogsApi;
import com.kgc.sbt.api.CouponReceiveApi;
import com.kgc.sbt.api.VipUserApi;
import com.kgc.sbt.config.annotation.RequestPermission;
import com.kgc.sbt.domain.Coupon;
import com.kgc.sbt.domain.CouponReceive;
import com.kgc.sbt.util.RedisUtils;
import com.kgc.sbt.util.RequestResult;
import com.kgc.sbt.util.ResultBuildUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author Shan
 * @date 2021/6/22
 * @return
 * @description 优惠券业务入口
 */
@RestController
@Slf4j
@Api(tags = "优惠券业务操作入口，版本号：1.0.0")
public class CouponController {

    @Reference
    private CouponApi couponApi;

    @Reference
    private CouponReceiveApi couponReceiveApi;

    @Reference
    private CouponLogsApi couponLogsApi;

    @Reference
    private VipUserApi vipUserApi;

    @Autowired
    private RedisUtils redisUtils;
    /**
     * @return
     * @author Shan
     * @date 2021/6/22
     * @description 领取优惠券接口
     */
    @PostMapping("/couponReceive")
    @RequestPermission
    @ApiOperation(value = "领取优惠券操作", httpMethod = "POST", notes = "必传用户ID、优惠券ID")
    public RequestResult<String> couponReceive(HttpServletRequest request,
                                               @ApiParam(required = true,name = "couponId",value = "优惠券ID")@RequestParam Integer couponId) {

        // 校验权限，是否已经登录
        String token = request.getHeader("token");

        // 校验token是否有效
        Integer userId = (Integer)redisUtils.get(token);

        if (couponReceiveApi.queryCouponByUserIdAndCouponId(userId,couponId)){
            return ResultBuildUtil.fail("4001","领取失败！此优惠券只可领取一张！");
        }
        Coupon coupon=couponApi.queryCouponById(couponId);

        if (coupon == null){
            return ResultBuildUtil.fail("4001","没有此优惠券！");
        }

        CouponReceive couponReceive = new CouponReceive();
        //用户id
        couponReceive.setUserId(userId);
        //优惠券id
        couponReceive.setCouponId(couponId);
        //状态
        couponReceive.setStatus(0);
        //扣除积分
        Integer score=coupon.getScore();
        if (!vipUserApi.reduceScore(userId,score)){
            return ResultBuildUtil.fail("4001","领取失败！积分不足！");
        }

        couponReceive.setScore(score);
        //领取时间
        couponReceive.setCreateTime(new Date());

        //减优惠券数量
        coupon.setCount(coupon.getCount()-1);
        coupon.setTakeCount(coupon.getTakeCount()+1);
        couponApi.updateCouponCountById(coupon);
        couponReceiveApi.insertCouponReceive(couponReceive);
        //插入数据库
        return ResultBuildUtil.success("优惠券领取成功！");
    }


    /**
     * @return
     * @author Shan
     * @date 2021/6/22
     * @description 查询优惠券接口
     */
    @ApiOperation(value = "查询优惠券操作", httpMethod = "GET")
    @GetMapping("/findcCoupon")
    public RequestResult<List<Coupon>> findcCoupon() {
        return ResultBuildUtil.success(couponApi.queryAllCoupon());
    }


    /**
     * @author Shan
     * @date 2021/6/22
     * @return
     * @description 优惠券消费记录接口
     */
    @PutMapping("/couponLogs")
    @RequestPermission
    @ApiOperation(value = "优惠券消费记录操作入口", httpMethod = "GET",notes = "内部接口，使用时调用")
    public RequestResult<String> couponLogs(HttpServletRequest request,
                                            @ApiParam(required = true,name = "couponId",value = "优惠券id")@RequestParam Integer couponId,
                                            @ApiParam(required = true,name = "orderNumber",value = "订单号")@RequestParam String orderNumber){
        // 校验权限，是否已经登录
        String token = request.getHeader("token");

        // 校验token是否有效
        Integer userId = (Integer)redisUtils.get(token);

        couponLogsApi.insertCouponLogs(userId,couponId,orderNumber);

        return ResultBuildUtil.success("数据操作成功！");
    }

    /**
     * @author Shan
     * @date 2021/6/24
     * @return
     * @description 更新优惠券使用情况
     */
    @PutMapping("/modCouponReceive")
    @RequestPermission
    @ApiOperation(value = "更新优惠券状态操作",  notes = "内部接口，使用时调用")
    public RequestResult<String> modCouponReceive(HttpServletRequest request,
                                                  @ApiParam(required = true,name = "couponId",value = "优惠券id")@RequestParam Integer couponId) {

        // 校验权限，是否已经登录
        String token = request.getHeader("token");

        // 校验token是否有效
        Integer userId = (Integer)redisUtils.get(token);
        if (couponReceiveApi.modCouponReceive(userId,couponId)){
            return ResultBuildUtil.success("优惠券状态更新成功");
        }

        return ResultBuildUtil.fail("5001","抱歉！优惠券不存在！");
    }



}
